<template>
  <div class="message-contianer">
    <div class="message-top">
      <div class="title">消息中心</div>
    </div>
    <div class="message-bottom">
      <div class="bottom-top">
        <div class="top-text">
          共 {{ total }} 条消息，其中<span>未读消息</span>
          {{ Unread }} 条
        </div>
        <el-button type="primary" size="small">
          <div style="display: flex; align-items: center">
            <img
                src="@/assets/images/clearmessage.png"
                alt=""
                style="width: 14px; height: 14px"
            />
            <span style="margin-left: 5px" @click="handlerallread">
              全部已读</span
            >
          </div>
        </el-button>
      </div>
      <div class="bottom-bottom">
        <div
            class="item"
            v-for="(item, index) in messagelist"
            :key="index"
            @click="hangdlermessage(item)"
        >
          <div class="item-left">
            <div class="item-dot" v-if="item.readStatus == 0"></div>
            <div v-if="item.msgFlag == 0" class="msgFlag0">

            </div>
            <div v-if="item.msgFlag == 1" class="msgFlag1">

            </div>
            <div class="left-text">
              <div class="title">{{ item.msgTitle }}</div>
              <div class="text">{{ item.msgContent }}</div>
            </div>
          </div>
          <div class="item-right">{{ item.msgPushTime }}</div>
        </div>
      </div>
        <!-- 分页 -->
        <pagination
            class="pagination"
            :total="total"
            v-model:page="params.pageNum"
            v-model:limit="params.pageSize"
            @pagination="getMessagelist"
        />
    </div>
  </div>
</template>
<script setup>
import {ref, onMounted, nextTick} from "vue";
import {
  gethistMessagelist,
  updateAllRead,
  getMessageNumber, infoMsgAlone
} from "@/api/message/message";
import useMessageStore from "@/store/modules/message";
import { useRouter } from "vue-router";
import {getDicts} from "@/api/system/dict/data";
const router = useRouter();
const messageStore = useMessageStore();

// 搜索参数
let params = ref({
  pageNum: 1,
  pageSize: 10,
});

let messagelist = ref([]);
let total = ref(0);
const loading = ref(false);

/**
 * @description: 表格数据接口
 */
const getMessagelist = () => {
  loading.value = true;
  gethistMessagelist(params.value).then((res) => {
    messagelist.value = res.data.records;
    total.value = res.data.total;
    loading.value = false;
  });
};

/**
 * @description: 全部已读
 */
const handlerallread = () => {
  updateAllRead().then((res) => {
    if (res.code == 200) {
      getMessagelist();
      getMessageNumberFun();
    }
  });
};
/**
 * @description: 处理消息
 */
const hangdlermessage = (item) => {
  switch (item.msgType) {
    case 2:
      if (item.msgFlag == 0) {
        editMessage(item);
      }
      router.push({
        path: "/personnelControl",
      });
      break;
    case 1:
      if (item.msgFlag == 0) {
        editMessage(item);
      }
      router.push({
        path: "/orderVerification",
      });
      break;
    case 3:
      if (item.msgFlag == 0) {
        editMessage(item);
      }
      router.push({
        path: "/",
      });
      break;
    default:
      break;
  }
};

/**
 * @description: 已读单条信息
 */
const editMessage = (item) => {
  infoMsgAlone({
    msgId: item.id
  }).then((res) => {
    if (res.code == 200) {
      getMessagelist();
      getMessageNumberFun();
    }
  });
};

let Unread = 0;
const getMessageNumberFun = () => {
  getMessageNumber().then((res) => {
    if (res.code === 200) {
      Unread = res.data;
      messageStore.messagenum = res.data;
    }
  })
}

onMounted(() => {
  getMessagelist();
  getMessageNumberFun();
});
</script>

<style scoped lang="scss">
.message-contianer {
  .message-top {
    display: flex;
    align-items: center;
    height: 120px;
    background: url("@/assets/images/message-bac.png") no-repeat;
    background-size: 100% 100%;
    margin-bottom: 20px;
    .title {
      font-weight: 500;
      font-size: 30px;
      color: #001528;
      margin-left: 45px;
    }
  }
  .message-bottom {
    height: 67vh;
    background-color: #fff;
    border-radius: 10px;
    padding: 0 20px;
    .bottom-top {
      display: flex;
      justify-content: space-between;
      width: 100%;
      padding: 20px 0;
      border-bottom: 1px solid #f6f6f6;
    }
    .bottom-bottom {
      height: 51vh;
      overflow-y: scroll;
      .item {
        display: flex;
        justify-content: space-between;
        padding: 20px;
        border-bottom: 1px solid #f6f6f6;
        cursor: pointer;
        .item-left {
          position: relative;
          display: flex;
          align-items: center;
          .left-text {
            margin-left: 10px;
            .title {
              font-weight: bold;
              font-size: 16px;
              color: #001528;
              margin-bottom: 8px;
            }
          }
          .item-dot {
            position: absolute;
            top: 7px;
            width: 10px;
            height: 10px;
            background: #ff3e3e;
            border-radius: 50%;
          }
        }
        .item-right {
          font-size: 14px;
          color: #898989;
        }
      }
    }
  }
  .msgFlag0{
    width: 10px;
    height: 10px;
    background-color: #ff1f39;
    border-radius: 5px;
  }
  .msgFlag1{
    width: 10px;
    height: 10px;
    background-color: #e6e6e6;
    border-radius: 5px;
  }
}
</style>
